<?php

/**
 *    我的地址控制器
 *
 *    @author    Garbin
 *    @usage    none
 */
class My_addressApp extends MemberbaseApp
{
    function index()
    {
        /* 取得列表数据 */
        $model_address =& m('address');
        $addresses     = $model_address->find(array(
            'conditions'    => 'user_id = ' . $this->visitor->get('user_id'),
			'order'      => 'case when default_use>0 then 0 else 1 end, default_use DESC, addr_id DESC',
        ));
		//print_r($addresses);
        $this->assign('addresses', $addresses);
		
		if ( !$addresses )
		{
			if ( $_GET['for_cart'] )//返回购物车去
			{
				header('location:index.php?app=my_address&act=add&for_cart='.$_GET['for_cart']);
			}else{
				header('location:index.php?app=my_address&act=add');
			}
		}

        $this->_config_seo('title', '地址薄 - ' . Conf::get('site_name'));
		$this->assign('curitem', '地址薄');	
        $this->display('my_address.index.html');
    }

    /**
     *    添加地址
     *
     *    @author    Garbin
     *    @return    void
     */
    function add()
    {
        if (!IS_POST)
        {
            $this->assign('act', 'add');
            $this->_get_regions();

			$this->_config_seo('title', '添加地址 - ' . Conf::get('site_name'));
			$this->assign('curitem', '添加地址');				
            $this->display('my_address.form.html');
        }
        else
        {
            /* 电话和手机至少填一项 */
            if (!$_POST['phone_mob'])
            {
                $this->show_warning('请正确输入手机号码');
                return;
            }

            $region_name = $_POST['region_name'];
            $data = array(
                'user_id'       => $this->visitor->get('user_id'),
                'ddl_type'      => htmldecode($_POST['ddl_type']),
				'consignee'     => htmldecode($_POST['consignee']),
                'region_id'     => htmldecode($_POST['region_id']),
                'region_name'   => htmldecode($_POST['region_name']),
                'address'       => htmldecode($_POST['address']),
                'zipcode'       => htmldecode($_POST['zipcode']),
                'phone_tel'     => htmldecode($_POST['phone_tel']),
                'phone_mob'     => htmldecode($_POST['phone_mob']),
				'txt_countrys'  => htmldecode($_POST['txt_country']).'//'.htmldecode($_POST['txt_province']).'//'.htmldecode($_POST['txt_city']).'//'.htmldecode($_POST['txtqu']),//国家/地区/省/市,
				'idpic'         => htmldecode($_POST['idpic']),
				'txtidcode'     => htmldecode($_POST['txtidcode']),
				'txt_email'     => htmldecode($_POST['txt_email']),
				'txt_company'   => htmldecode($_POST['txt_company']),
				'addtime'   => gmtime()
				);
			//如果是从购物车来的，并且地址是空的时候
			$_GET['sh_null'] == 1 && $data['default_use'] = 1;
			
            $model_address =& m('address');
            if (!($address_id = $model_address->add($data)))
            {
                $this->show_warning($model_address->get_error());
                return;
            }
			$this->write_log($this->visitor->get('user_id'), $this->visitor->get('user_name'), '【手机】添加地址,ID:'.$address_id, 0);//添加操作日志	
			
            $this->show_message('添加成功','back_list', 'index.php?app=my_address' );	
			//如果是从购物车来的，并且地址是空的时候
//			if ( $_GET['sh_null'] == 1)
//			{
//				header('location:index.php?app=order&rec_id='.$_GET['for_cart']);
//				exit;
//			}
//
//			if ( $_GET['for_cart'] )//返回购物车去
//			{
//				header('location:index.php?app=my_address&for_cart='.$_GET['for_cart']);
//			}else{
//				header('location:index.php?app=my_address');
//			}
            
        }
    }
    function edit()
    {
        $addr_id = empty($_GET['addr_id']) ? 0 : intval($_GET['addr_id']);
        if (!$addr_id)
        {
			$this->show_warning('没有该地址');
            return;
        }
        if (!IS_POST)
        {
            $model_address =& m('address');
            $find_data     = $model_address->find("addr_id = {$addr_id} AND user_id=" . $this->visitor->get('user_id'));
            if (empty($find_data))
            {
                $this->show_warning('没有该地址');
                return;
            }
            $address = current($find_data);
			//四、填写发货地址 五、填写地址
			$countrys1 = explode('//',$address['txt_countrys']);
			$address['txt_country'] = $countrys1[0];
			$address['txt_province'] = $countrys1[1];
			$address['txt_city'] = $countrys1[2];
			$address['txtqu'] = $countrys1[3];
			
            $this->assign('address', $address);
            $this->assign('act', 'edit');
            //$this->_get_regions();
			$this->_config_seo('title', Lang::get('edit_address') . ' - ' . Lang::get('member_center') );
			$this->assign('curitem', '修改地址');	
            $this->display('my_address.form.html');
        }
        else
        {
            /* 电话和手机至少填一项 */
            if (!$_POST['phone_tel'] && !$_POST['phone_mob'])
            {
                $this->show_warning('请正确输入手机号码');
                return;
            }
            $data = array(
                'ddl_type'      => htmldecode($_POST['ddl_type']),
				'consignee'     => htmldecode($_POST['consignee']),
                'region_id'     => htmldecode($_POST['region_id']),
                'region_name'   => htmldecode($_POST['region_name']),
                'address'       => htmldecode($_POST['address']),
                'zipcode'       => htmldecode($_POST['zipcode']),
                'phone_tel'     => htmldecode($_POST['phone_tel']),
                'phone_mob'     => htmldecode($_POST['phone_mob']),
				'txt_countrys'  => htmldecode($_POST['txt_country']).'//'.htmldecode($_POST['txt_province']).'//'.htmldecode($_POST['txt_city']).'//'.htmldecode($_POST['txtqu']),//国家/地区/省/市,
				'idpic'         => htmldecode($_POST['idpic']),
				'txtidcode'     => htmldecode($_POST['txtidcode']),
				'txt_email'     => htmldecode($_POST['txt_email']),
				'txt_company'   => htmldecode($_POST['txt_company']),
            );
            $model_address =& m('address');
            $model_address->edit("addr_id = {$addr_id} AND user_id=" . $this->visitor->get('user_id'), $data);
            if ($model_address->has_error())
            {
                $this->show_warning($model_address->get_error());
                return;
            }
			$this->write_log($this->visitor->get('user_id'), $this->visitor->get('user_name'), '【手机】修改地址,ID:'.$addr_id, 0);//添加操作日志

            $this->show_message('编辑成功','back_list', 'index.php?app=my_address');	
//			if ( $_GET['for_cart'] )//返回购物车去
//			{
//				header('location:index.php?app=my_address&for_cart='.$_GET['for_cart']);
//			}else{
//				header('location:index.php?app=my_address');
//			}
        }
    }

	//删除
    function drop()
    {
        $addr_id = isset($_GET['addr_id']) ? htmldecode($_GET['addr_id']) : 0;
        if (!$addr_id)
        {
            $this->show_warning('没有该地址');
            return;
        }
        $ids = explode(',', $addr_id);//获取一个类似array(1, 2, 3)的数组
        $model_address  =& m('address');
        $drop_count = $model_address->drop("user_id = " . $this->visitor->get('user_id') . " AND addr_id " . db_create_in($ids));
        if (!$drop_count)
        {
            /* 没有可删除的项 */
            $this->show_warning('没有该地址');
            return;
        }

        if ($model_address->has_error())    //出错了
        {
            $this->show_warning($model_address->get_error());
            return;
        }

		if ( $_GET['for_cart'] )//
		{
			header('location:index.php?app=my_address&for_cart='.$_GET['for_cart']);
		}else{
			header('location:index.php?app=my_address');
		}
    }
	
	//设为默认地址
	function set()
	{
        $addr_id = isset($_GET['addr_id']) ? htmldecode($_GET['addr_id']) : 0;
        if (!$addr_id)
        {
            $this->show_warning('没有该地址');
            return;
        }
		$model_address =& m('address');
		$model_address->edit("user_id=" . $this->visitor->get('user_id'), "default_use = 0");
        $model_address->edit("addr_id = {$addr_id} AND user_id=" . $this->visitor->get('user_id'), "default_use = 1");

		if ( $_GET['for_cart'] )//返回购物车去
		{
			header('location:index.php?app=order&rec_id='.$_GET['for_cart']);
		}else{
			header('location:index.php?app=my_address');
		}		
	}
		
    function _get_regions()
    {
        $model_region =& m('region');
        $regions = $model_region->get_list(1);
        if ($regions)
        {
            $tmp  = array();
            foreach ($regions as $key => $value)
            {
                $tmp[$key] = $value['region_name'];
            }
            $regions = $tmp;
        }
        $this->assign('regions', $regions);
    }
	
	//ajax请求下级地区
	function mlselection()
	{
		$pid = empty($_GET['pid']) ? 0 : $_GET['pid'];
		$mod_region =& m('region');
		$regions = $mod_region->get_list($pid);
		foreach ($regions as $key => $region)
		{
			$regions[$key]['region_name'] = htmlspecialchars($region['region_name']);
		}
		$this->json_result(array_values($regions));
	}

}

?>